package com.cdtu.labpreview.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cdtu.labpreview.common.Result;
import com.cdtu.labpreview.controller.vo.ContentPreviewVo;
import com.cdtu.labpreview.entity.BaseIntroduction;
import com.cdtu.labpreview.entity.Class;
import com.cdtu.labpreview.entity.ContentPreview;
import com.cdtu.labpreview.entity.Student;
import com.cdtu.labpreview.mapper.ClassMapper;
import com.cdtu.labpreview.mapper.ContentPreviewMapper;
import com.cdtu.labpreview.mapper.StudentMapper;
import com.cdtu.labpreview.mapper.TeacherClassMapper;
import com.cdtu.labpreview.service.IContentPreviewService;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class ContentPreviewServiceImpl extends ServiceImpl<ContentPreviewMapper, ContentPreview> implements IContentPreviewService {

    @Autowired
    private ContentPreviewMapper contentPreviewMapper;

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private ClassMapper classMapper;
    @Autowired
    private TeacherClassMapper teacherClassMapper;

    @Override
    public Result getContentPreviewOfTeacher(int tno) {
        LambdaQueryWrapper<ContentPreview> lqw=new LambdaQueryWrapper<>();
        lqw.eq(ContentPreview::getTno,tno);
        List<ContentPreview> contentPreviews = contentPreviewMapper.selectList(lqw);
        List<ContentPreviewVo> contentPreviewVoList = contentPreviews.stream().map(contentPreview -> {
            ContentPreviewVo cpv = new ContentPreviewVo();
            BeanUtils.copyProperties(contentPreview, cpv);
            long classId = contentPreview.getClassId();
            Class aClass = classMapper.selectById(classId);
            String cname = aClass.getName();
            cpv.setClassName(cname);
            return cpv;
        }).collect(Collectors.toList());
        return Result.ok(contentPreviewVoList);
    }

    @Override
    public Result getContentPreviewOfStudent(int sno) {
        //根据学号获取班级id
        LambdaQueryWrapper<Student> lqw=new LambdaQueryWrapper<>();
        lqw.eq(Student::getSno,sno);
        Student student = studentMapper.selectOne(lqw);
        long classId = student.getClassId();
        //根据班级id获取对应老师发布的实验预习内容
        LambdaQueryWrapper<ContentPreview> lqw2=new LambdaQueryWrapper<>();
        lqw2.eq(ContentPreview::getClassId,classId);
        List<ContentPreview> contentPreviews = contentPreviewMapper.selectList(lqw2);
        return Result.ok(contentPreviews);
    }
}
